package com.qianying.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qianying.comment.CommonResult;
import com.qianying.entity.QuoteItem;
import com.qianying.mapper.QuoteItemMapper;
import com.qianying.service.QuoteItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import java.util.List;

/**
 * 报价项目服务实现类
 * 实现报价项目相关的业务逻辑
 */
@Service
public class QuoteItemServiceImpl extends ServiceImpl<QuoteItemMapper, QuoteItem> implements QuoteItemService {

    @Autowired
    private QuoteItemMapper quoteItemMapper;

    /**
     * 分页查询报价项目列表
     */
    @Override
    public CommonResult<IPage<QuoteItem>> getPage(Page<QuoteItem> page, String itemName) {
        LambdaQueryWrapper<QuoteItem> queryWrapper = new LambdaQueryWrapper<>();
        // 模糊查询条件
        if (StringUtils.hasText(itemName)) {
            queryWrapper.like(QuoteItem::getItemName, itemName);
        }
        // 按ID升序排序
        queryWrapper.orderByAsc(QuoteItem::getId);
        // 执行分页查询
        IPage<QuoteItem> resultPage = quoteItemMapper.selectPage(page, queryWrapper);
        return CommonResult.success(resultPage);
    }

    /**
     * 根据ID查询报价项目详情
     */
    @Override
    public CommonResult<QuoteItem> getById(Long id) {
        // 空值校验
        if (id == null) {
            return CommonResult.failed("ID不能为空");
        }
        // 查询数据
        QuoteItem quoteItem = quoteItemMapper.selectById(id);
        if (quoteItem == null) {
            return CommonResult.failed("未找到ID为" + id + "的报价项目");
        }
        return CommonResult.success(quoteItem);
    }

    /**
     * 新增报价项目
     */
    @Override
    public CommonResult<?> saveItem(QuoteItem quoteItem) {
        // 空值校验
        if (quoteItem == null) {
            return CommonResult.failed("提交的数据不能为空");
        }
        // 主键ID必须为空（自增）
        if (quoteItem.getId() != null) {
            return CommonResult.failed("新增操作ID必须为空");
        }
        // 名称不能为空
        if (!StringUtils.hasText(quoteItem.getItemName())) {
            return CommonResult.failed("报价项目名称不能为空");
        }

        // 校验名称唯一性
        LambdaQueryWrapper<QuoteItem> nameWrapper = new LambdaQueryWrapper<>();
        nameWrapper.eq(QuoteItem::getItemName, quoteItem.getItemName());
        if (quoteItemMapper.selectCount(nameWrapper) > 0) {
            return CommonResult.failed("报价项目名称已存在");
        }
        quoteItem.setId(null);
        // 执行新增
        int rows = quoteItemMapper.insert(quoteItem);
        return rows > 0 ? CommonResult.success("新增报价项目成功") : CommonResult.failed("新增报价项目失败");
    }

    /**
     * 更新报价项目
     */
    @Override
    public CommonResult<?> updateItem(QuoteItem quoteItem) {
        // 空值校验
        if (quoteItem == null) {
            return CommonResult.failed("提交的数据不能为空");
        }
        // ID不能为空
        if (quoteItem.getId() == null) {
            return CommonResult.failed("ID不能为空");
        }
        // 名称不能为空
        if (!StringUtils.hasText(quoteItem.getItemName())) {
            return CommonResult.failed("报价项目名称不能为空");
        }

        // 校验数据存在性
        QuoteItem existing = quoteItemMapper.selectById(quoteItem.getId());
        if (existing == null) {
            return CommonResult.failed("未找到ID为" + quoteItem.getId() + "的报价项目");
        }

        // 校验名称唯一性（排除当前ID）
        LambdaQueryWrapper<QuoteItem> nameWrapper = new LambdaQueryWrapper<>();
        nameWrapper.eq(QuoteItem::getItemName, quoteItem.getItemName())
                .ne(QuoteItem::getId, quoteItem.getId());
        if (quoteItemMapper.selectCount(nameWrapper) > 0) {
            return CommonResult.failed("报价项目名称已存在");
        }

        // 执行更新
        int rows = quoteItemMapper.updateById(quoteItem);
        return rows > 0 ? CommonResult.success("更新报价项目成功") : CommonResult.failed("更新报价项目失败");
    }

    /**
     * 根据ID删除报价项目
     */
    @Override
    public CommonResult<?> removeById(Long id) {
        // 空值校验
        if (id == null) {
            return CommonResult.failed("ID不能为空");
        }
        // 校验数据存在性
        QuoteItem existing = quoteItemMapper.selectById(id);
        if (existing == null) {
            return CommonResult.failed("未找到ID为" + id + "的报价项目");
        }
        // 执行删除
        int rows = quoteItemMapper.deleteById(id);
        return rows > 0 ? CommonResult.success("删除报价项目成功") : CommonResult.failed("删除报价项目失败");
    }

    /**
     * 查询所有报价项目（不分页）
     */
    @Override
    public CommonResult<List<QuoteItem>> getAll() {
        LambdaQueryWrapper<QuoteItem> queryWrapper = new LambdaQueryWrapper<>();
        // 按ID升序排序
        queryWrapper.orderByAsc(QuoteItem::getId);
        // 查询所有数据
        List<QuoteItem> quoteItemList = quoteItemMapper.selectList(queryWrapper);
        return CommonResult.success(quoteItemList);
    }
}